/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ipccenter.war;

import javax.naming.InitialContext;
import org.apache.log4j.Logger;
import ru.ipccenter.ejb.UserInterface;

/**
 *
 * @author nick
 */
public class MailCheck extends Check {
    private UserInterface ui = null;
//    private static final Logger logger = Logger.getLogger(MailCheck.class);

    @Override
    public Result check() {
        try {
            InitialContext ic = new InitialContext();
            ui = (UserInterface) ic.lookup("UserSession");
        } catch (Exception ex) {
            System.out.println("Error:"
                    + ex.getMessage());
            //logger.error(ex);
        }
        if(super.check() != Result.SUCCESS) {
            return super.check();
        }
        if (!parameter.matches("[A-Za-z0-9][\\w.-]{0,20}[A-Za-z0-9]@[[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9].]+(ru|com|net|org)")) {
            return Result.INCORRECTMAIL;
        }
        if (ui.checkForExistMail(parameter)) {
            return Result.EXISTMAIL;
        }
        return Result.SUCCESS;
    }
}